package com.ruoyi.activiti.bpmnparse.x6translator;

import com.ruoyi.activiti.bpmnparse.Translator;
import com.ruoyi.common.utils.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *   对应的 x6 json文件
 *     {
 *         "isMulti": "1",
 *         "label": "会签",
 *         "status": "success",
 *         "bpmnType": "userMultiTask",
 *         "id": "hrVerify",
 *         "rejectStrategy": "1",
 *         "rejectValue": "1",
 *         "approvedStrategy": "2",
 *         "approvedValue": "0.5"
 *       }
 *    对应的xml文件
 *    <userTask id="a5905e9fb-74ad-4fd2-b0cb-2009f34920bc" name="My Task" activiti:assignee="${assignee}">
 * 	     <extensionElements>
 *         <activiti:executionListener event="start" expression="${execution.setVariable('approvedCounter', 0)}"></activiti:executionListener>
 *  	   <activiti:executionListener event="start" expression="${execution.setVariable('rejectedCounter', 0)}"></activiti:executionListener>
 *         <activiti:taskListener event="complete" class="com.ruoyi.activiti.listener.CounterSignCompleteListener"></activiti:taskListener>
 *       </extensionElements>
 *       <multiInstanceLoopCharacteristics isSequential="false"
 *                                         activiti:collection="assigneeList" activiti:elementVariable="assignee" >
 *         <completionCondition>${(approvedCounter/nrOfInstances >= 0.5) || (rejectedCounter == 1) }</completionCondition>
 *       </multiInstanceLoopCharacteristics>
 *     </userTask>
 */
public class MultiUserTaskTranslator extends Translator {
    public final static String STRATEGY_COUNT = "1";
    public final static String STRATEGY_PERCENT = "2";
    private List<Map<String,Object>> x6ListMap;
    private final String type = "userTask";
    public MultiUserTaskTranslator(List<Map<String,Object>> x6ListMap){
        this.x6ListMap = x6ListMap;
    }
    public String getTaskType(){
        return type;
    }
    @Override
    public Map<String, Object> translate(Map<String, Object> sourceMap) {
        Map<String, Object> dataMap = (Map<String, Object>) sourceMap.get("data");
        Map<String, Object> bpmnMap = new HashMap<>();
        String id = (String) dataMap.get("id");
        String name = (String) dataMap.get("label");
        String assignee = "${assignee}";
        String extensionElementsValue =
                " <activiti:executionListener event=\"start\" expression=\"${execution.setVariable('approvedCounter', 0)}\"></activiti:executionListener>\n" +
                " <activiti:executionListener event=\"start\" expression=\"${execution.setVariable('rejectedCounter', 0)}\"></activiti:executionListener>\n" +
                        " <activiti:executionListener event=\"start\" delegateExpression=\"${multiExecutionListener}\"></activiti:executionListener>\n" +
                        " <activiti:taskListener event=\"complete\" delegateExpression=\"${counterSignCompleteListener}\"></activiti:taskListener>";
        Map<String,Object> extensionElements = new HashMap<>();
        extensionElements.put("value",extensionElementsValue);

        Map<String,Object> multiInstance = new HashMap<>();
        multiInstance.put("isSequential",false);
        multiInstance.put("activiti:collection","assigneeList");
        multiInstance.put("activiti:elementVariable","assignee");
        String appendValue = getMultiInstanceValue(dataMap);
        String multiInstanceValue = "<completionCondition>${"+appendValue+"}</completionCondition>";
        multiInstance.put("value",multiInstanceValue);
        bpmnMap.put(Translator.BPMN_TYPE,type);
        bpmnMap.put(Translator.BPMN_ID,id);
        bpmnMap.put(Translator.BPMN_NAME,name);
        bpmnMap.put(Translator.BPMN_ASSIGNEE,assignee);
        bpmnMap.put(Translator.BPMN_EXTENSION_ELE,extensionElements);
        bpmnMap.put(Translator.BPMN_MULTI_INSTANCE,multiInstance);
        return bpmnMap;
    }
    private String getMultiInstanceValue(Map<String, Object> dataMap){
        String rejectStrategy = (String) dataMap.get("rejectStrategy");
        String rejectValue = (String) dataMap.get("rejectValue");
        String approvedStrategy = (String) dataMap.get("approvedStrategy");
        String approvedValue = (String) dataMap.get("approvedValue");
        StringBuilder reject = new StringBuilder();
        if(STRATEGY_COUNT.equals(rejectStrategy)) {
            reject =reject.append("rejectedCounter >= ").append(rejectValue);
        } else if(STRATEGY_PERCENT.equals(rejectStrategy)) {
            reject =reject.append("rejectedCounter/nrOfInstances >= ").append(rejectValue);
        }
        StringBuilder approve = new StringBuilder();
        if(STRATEGY_COUNT.equals(approvedStrategy)) {
            approve =approve.append("approvedCounter >= ").append(approvedValue);
        } else if(STRATEGY_PERCENT.equals(approvedStrategy)) {
            approve =approve.append("approvedCounter/nrOfInstances >= ").append(approvedValue);
        }
        String rejectStr = reject.toString();
        String approveStr = approve.toString();
        if(StringUtils.isNotEmpty(rejectStr) && StringUtils.isNotEmpty(approveStr)){
            return approveStr + " || " + rejectStr;
        } else if(StringUtils.isNotEmpty(rejectStr)) {
            return rejectStr;
        } else {
            return approveStr;
        }
    }
}
